package com.xavier.book.spring_book_demo.controller;

import com.xavier.book.spring_book_demo.model.*;
import com.xavier.book.spring_book_demo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/addBook")
    public Result<String> addBook(BookInfo bookInfo) {
        log.info("添加图书，request:{}" + bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null
        ){
        log.warn("添加图书，参数不合法，request:{}",bookInfo);
        return Result.fail("参数不合法");
        }
        try {
            bookService.addBook(bookInfo);
            return Result.sucess("");
        } catch (Exception e) {
            log.error("添加图书异常，e",e);
            return Result.fail("添加图书发生异常");
        }
    }
    @RequestMapping("/getListByPage")
    public Result<ResponseResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session) {
//        if (session.getAttribute(Constants.SESSION_USER_KEY) == null) {
//            return Result.unlogin();
//        }
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo == null || userInfo.getId() <= 0) {
//            return Result.unlogin();
//        }
        ResponseResult<BookInfo> listMyPage = bookService.getListMyPage(pageRequest);
        return Result.sucess(listMyPage);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("查询图书信息,bookId" + bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
    public Result<String> updateBook(BookInfo bookInfo) {
        log.info("修改图书:{}", bookInfo);
        try {
            bookService.updateBook(bookInfo);
            return Result.sucess("");
        } catch (Exception e) {
            log.error("修改图书失败", e);
            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping("/batchDeleteBook")
    public boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids:{}",ids);
        try{
            bookService.batchDeleteBook(ids);
        }catch (Exception e){
            log.error("批量删除异常,e:",e);
            return false;
        }
        return true;
    }

    // 兼容前端 /book/batchDelete 路径
    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids){
        return batchDeleteBook(ids);
    }

    @RequestMapping("/deleteBook")
    public Result<String> deleteBook(@RequestParam("bookId") Integer bookId){
        log.info("删除图书, id:{}", bookId);
        try{
            bookService.deleteBookById(bookId);
            return Result.sucess(""); // 前端期望 data 为空串
        }catch (Exception e){
            log.error("删除图书异常,e:", e);
            return Result.fail("删除失败");
        }
    }
}
